import subprocess
import sys

nginxPath = '/usr/local/nginx/sbin/nginx'

def StartNginx():
    result = subprocess.run(['sudo', nginxPath], check=True)
    if result.returncode == 0:
        print('Nginx is started.')
    else:
        print('Nginx is failed to start.')

def StopNginx():
    result = subprocess.run(['sudo', nginxPath, '-s', 'stop'], check=True)
    if result.returncode == 0:
        print('Nginx is stopped.')
    else:
        print('Nginx is failed to stop.')

def ReloadNginx():
    result = subprocess.run(['sudo', nginxPath, '-s', 'reload'], check=True)
    if result.returncode == 0:
        print('Nginx is reloaded.')
    else:
        print('Nginx is failed to reload.')

def main():
    if len(sys.argv) < 2:
        print('Please input the operation.')
        sys.exit(0)

    operation = sys.argv[1]
    if operation == 'start':
        StartNginx()
    elif operation == 'stop':
        StopNginx()
    elif operation == 'reload':
        ReloadNginx()
    else:
        print('Operation is failed, please input the correct operation.')
        sys.exit(0)


if __name__ == '__main__':
    main()